package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.TextView;

import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerSettingComponent;
import com.ewzj.bigdata.di.module.SettingModule;
import com.ewzj.bigdata.mvp.contract.SettingContract;
import com.ewzj.bigdata.mvp.presenter.SettingPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;

import butterknife.BindView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class SettingActivity extends BaseActivity<SettingPresenter> implements SettingContract.View {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.st_push)
    SwitchCompat stPush;
    @BindView(R.id.st_msg)
    SwitchCompat stMsg;
    @BindView(R.id.tv_cacesize)
    TextView tvCacesize;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSettingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .settingModule(new SettingModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_setting; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
//        tvCacesize.setText(Apputils.FormetFileSize(DataHelper.getDirSize(Glide.getPhotoCacheDir(this))));
        tvCacesize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMessage("12345678");
            }
        });
    }

    @Override
    public boolean useFragment() {
        return false;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    private void showDelCache() {
        new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("确认删除缓存文件？")
                .setContentText("删除后数据不能恢复，请留意!")
                .setCancelText("取消")
                .setConfirmText("确认")
                .showCancelButton(true)
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.cancel();
                    }
                })
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        ArmsUtils.obtainAppComponentFromContext(SettingActivity.this).imageLoader().clear(SettingActivity.this,
                                ImageConfigImpl.builder().isClearDiskCache(true).isClearMemory(true).build());
                        sweetAlertDialog.cancel();
                    }
                })
                .show();
    }


    @OnClick({R.id.rl_cache, R.id.rl_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_cache:
                showDelCache();
                break;
            case R.id.rl_loginout:
                showLoginOutView();
                break;
        }
    }

    private void showLoginOutView(){
        new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("退出登录？")
                .setContentText("您确认要退出登录吗？")
                .setCancelText("取消")
                .setConfirmText("确认")
                .showCancelButton(true)
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.cancel();
                    }
                })
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        DataHelper.setStringSF(SettingActivity.this, AppConstants.APP_TOKEN,"");
                        finish();
                    }
                })
                .show();
    }
}

